﻿#region License

// PentaBiz - Sustainable Software Development Framework Copyright (C) 2013 Zoltán Csizmazia
// 
// This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

#endregion

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NuGet;
using Settings = PentaBiz.Studio.Loader.Properties.Settings;

namespace PentaBiz.Studio.Loader
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            StringBuilder s = new StringBuilder("Updating application from server");
            Console.WriteLine(s.ToString());

            var repo = PackageRepositoryFactory.Default.CreateRepository("http://pentabi-pc/packageserver/nuget/");

            Console.Clear();
            s.Append(".");
            Console.WriteLine(s.ToString());

            var packageManager = new PackageManager(
                repo,
                new DefaultPackagePathResolver("http://pentabi-pc/packageserver/nuget/"),
                new PhysicalFileSystem(@"C:\PentaBiz")
                );

            s = new StringBuilder("Locating application package");
            Console.Clear();
            Console.WriteLine(s.ToString());
            var package = Task.Run(() =>
                {
                    var t = Task.Run(() => repo.FindPackage("PentaBiz.Studio.Application"));

                    while (!t.IsCompleted)
                    {
                        s.Append(".");
                        Console.Clear();
                        Console.WriteLine(s.ToString());
                        Thread.Sleep(TimeSpan.FromSeconds(1));
                    }

                    return t.Result;
                }).Result;

            Console.Clear();
            Console.WriteLine(s.ToString());

            s = new StringBuilder("Installing latest application package");

            Task.Run(() =>
                {
                    var t = Task.Run(() => packageManager.InstallPackage(package, false, true));

                    Console.Clear();
                    while (!t.IsCompleted)
                    {
                        s.Append(".");
                        Console.Clear();
                        Console.WriteLine(s.ToString());
                        Thread.Sleep(TimeSpan.FromSeconds(1));
                    }
                }).Wait();

            Console.WriteLine("Starting application.");

            var app =
                packageManager.LocalRepository.FindPackage("PentaBiz.Studio.Application")
                              .GetFiles()
                              .First(f => f.Path.EndsWith("PentaBiz.Studio.Application.exe"))
                              .EffectivePath;


            var p = new Process
                {
                    StartInfo =
                        {
                            WorkingDirectory = Path.Combine(@"C:\PentaBiz", package.GetFullName().Replace(" ", ".")),
                            FileName = "PentaBiz.Studio.Application.exe"
                        }
                };
            p.Start();

            //Console.ReadKey();

            //Console.WriteLine("Installed!");
            //Console.Read();

            //packageManager.UninstallPackage(package);

            //Console.WriteLine("Uninstalled!");
            //Console.Read();
        }
    }
}